package com.zpark.express.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zpark.express.domain.Admin;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public  class JwtUtil {
     // private static  final  String UID="uid";
      private static  final  String SECRE="df4ghjk56lf7ertyu";
      private static  final  long EXPIRE=60*60*1000;

      /**
       * 参考：https://blog.csdn.net/wenxingchen/article/details/85095202
       * http://www.ruanyifeng.com/blog/2018/07/json_web_token-tutorial.html
      * @Desc: 生成用户token
      * @param:
      * @return:
      * @Author: GOGOGO
      * @Date: 2020/7/16
      */
      public static String createToken(Integer uid){
          Date nowDate = new Date();
          Date expireDate=new Date(nowDate.getTime()+EXPIRE*1000);
          //存放一些信息
          Map<String,Object> claims=new HashMap<>();
          claims.put("UID",uid);
          return Jwts.builder()
                  .setClaims(claims)//一些声明信息，用户信息啥的
                  .setIssuedAt(nowDate)//设置发布时间
                  .setExpiration(expireDate)//设置过期时间
                  .signWith(SignatureAlgorithm.HS512,SECRE) //使用啥算法来加密
                  .compact();
      }

      /**
      * @Desc: 产生token ,传过来的参数是一个admin
      * @Date: 2020/7/17
      */

      public static String createToken(Admin admin){
         /* admin.setAdminName("颤三");
          admin.setAdminId(2);*/
          Date nowDate=new Date();
          Date expireDate=new Date(nowDate.getTime()+EXPIRE*1000*1000);
          //存放用户的信息
          Map<String,Object> claims = new HashMap();
          claims.put("loginAdmin",admin);
          return Jwts.builder()
                  .setClaims(claims)
                  .setExpiration(expireDate)
                  .setIssuedAt(nowDate)
                  .signWith(SignatureAlgorithm.HS512,SECRE)
                  .compact();

      }

      /**
      * @Desc: 解析token
      * @param:
      * @return:
      * @Author: GOGOGO
      * @Date: 2020/7/16
      */
      public static Claims getClaim(String token){
          Claims claims=null;
          claims=Jwts.parser()
                  .setSigningKey(SECRE)
                  .parseClaimsJws(token)
                  .getBody();
       return claims;
      }

      /**
      * @Desc: 获取jwt的发布时间
       * *  @param:
       *
      * @return:
      * @Author: GOGOGO
      * @Date: 2020/7/16
      */
      public static Date getIssuedAt(String token){
          //getClaim方法里面返回的数据有设置jwt的发布时间
          return getClaim(token).getIssuedAt();
      }

      public static  Integer getUId(String token){
          return TypeUtil.caseToInt(getClaim(token).getId());

      }

      public static Admin getAdmin(String token){
          //报异常java.util.LinkedHashMap cannot be cast to com.zpark.express.domain.Admin
          //return (Admin) getClaim(token).get("loginAdmin");
          //参考  https://blog.csdn.net/Ai_Ting_Java/article/details/100987125
          Object loginAdmin = getClaim(token).get("loginAdmin");
          ObjectMapper objectMapper = new ObjectMapper();
          Admin admin = objectMapper.convertValue(loginAdmin, Admin.class);
          return admin;

            /* 不行  Map<Object,Object> admin= (Map<Object, Object>) loginAdmin;
         Admin admin1= (Admin) admin.get("loginAdmin");
         return admin1;*/

      }

      public static Boolean isExpired(String token){
          Date expiration = getClaim(token).getExpiration();
          //判断expiration是否在new Date()之前
          return expiration.before(new Date());
      }

//其实产生的jwt的方法是多样的，只不过是要用不用的加密算法加密和解密来保证信息安全
    //或者方便  维护服务端是无状态的
    public static void main(String[] args) {
        Admin admin = new Admin();
        admin.setAdminName("oo");
        admin.setAdminId(8);
        System.out.println(com.zpark.express.util.JwtUtil.createToken(admin));
        System.out.println(com.zpark.express.util.JwtUtil.getClaim("eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbkFkbWluIjp7ImFkbWluSWQiOjIsImFkbWluTmFtZSI6IumipOS4iSIsImFkbWluUHdkIjpudWxsLCJhZG1pblBob25lIjpudWxsLCJhZG1pbkVtYWlsIjpudWxsLCJhZG1pbkltYWdlIjpudWxsLCJhZG1pblN0YXRlIjpudWxsLCJhZG1pblBvd2VyIjpudWxsfSwiZXhwIjo1MTk0OTUxNjg1LCJpYXQiOjE1OTQ5NTE2ODV9.nX0qtJY4PDsOcu_FGWnCLPObSiHclGS-QOgQbBmGxbVSyjUlUeoqcy1Y08HEtOBj8dIJ-9ZvAEbVlSU-vjrqJw"));
        System.out.println(com.zpark.express.util.JwtUtil.getIssuedAt("eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbkFkbWluIjp7ImFkbWluSWQiOjIsImFkbWluTmFtZSI6IumipOS4iSIsImFkbWluUHdkIjpudWxsLCJhZG1pblBob25lIjpudWxsLCJhZG1pbkVtYWlsIjpudWxsLCJhZG1pbkltYWdlIjpudWxsLCJhZG1pblN0YXRlIjpudWxsLCJhZG1pblBvd2VyIjpudWxsfSwiZXhwIjo1MTk0OTUxNjg1LCJpYXQiOjE1OTQ5NTE2ODV9.nX0qtJY4PDsOcu_FGWnCLPObSiHclGS-QOgQbBmGxbVSyjUlUeoqcy1Y08HEtOBj8dIJ-9ZvAEbVlSU-vjrqJw"));
        System.out.println(com.zpark.express.util.JwtUtil.isExpired("eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbkFkbWluIjp7ImFkbWluSWQiOjIsImFkbWluTmFtZSI6IumipOS4iSIsImFkbWluUHdkIjpudWxsLCJhZG1pblBob25lIjpudWxsLCJhZG1pbkVtYWlsIjpudWxsLCJhZG1pbkltYWdlIjpudWxsLCJhZG1pblN0YXRlIjpudWxsLCJhZG1pblBvd2VyIjpudWxsfSwiZXhwIjo1MTk0OTUxNjg1LCJpYXQiOjE1OTQ5NTE2ODV9.nX0qtJY4PDsOcu_FGWnCLPObSiHclGS-QOgQbBmGxbVSyjUlUeoqcy1Y08HEtOBj8dIJ-9ZvAEbVlSU-vjrqJw"));
        System.out.println(com.zpark.express.util.JwtUtil.getAdmin("eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbkFkbWluIjp7ImFkbWluSWQiOjIsImFkbWluTmFtZSI6IumipOS4iSIsImFkbWluUHdkIjpudWxsLCJhZG1pblBob25lIjpudWxsLCJhZG1pbkVtYWlsIjpudWxsLCJhZG1pbkltYWdlIjpudWxsLCJhZG1pblN0YXRlIjpudWxsLCJhZG1pblBvd2VyIjpudWxsfSwiZXhwIjo1MTk0OTUxNjg1LCJpYXQiOjE1OTQ5NTE2ODV9.nX0qtJY4PDsOcu_FGWnCLPObSiHclGS-QOgQbBmGxbVSyjUlUeoqcy1Y08HEtOBj8dIJ-9ZvAEbVlSU-vjrqJw"));

    }
}
